package com.waghost.bridalgown.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.waghost.bridalgown.dao.UserBgLikeDao;
import com.waghost.bridalgown.dao.UserInfoDao;
import com.waghost.bridalgown.dao.WorksDao;
import com.waghost.bridalgown.entity.Result;
import com.waghost.bridalgown.entity.UserBgLike;
import com.waghost.bridalgown.entity.UserInfo;
import com.waghost.bridalgown.entity.Works;
import com.waghost.bridalgown.service.UserBgLikeService;
import com.waghost.bridalgown.service.WorksService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@AllArgsConstructor
@Service
public class IWorksService implements WorksService {

    private WorksDao worksDao;
    private UserInfoDao userInfoDao;

    @Override
    public Result list() {
        List<Works> works = worksDao.selectList(null);
        for (Works work : works) {
            UserInfo userInfo = userInfoDao.selectById(work.getCreateUser());
            if (null != userInfo){
                work.setUsername(userInfo.getUsername());
            }
        }
        return Result.success(works);
    }

    @Override
    public Result getId(Integer id) {
        Works works = worksDao.selectById(id);
        UserInfo userInfo = userInfoDao.selectById(works.getCreateUser());
        if (null != userInfo){
            works.setUsername(userInfo.getUsername());
        }
        return Result.success(works);
    }

    @Override
    public Result add(@RequestBody Works works) {
        return Result.toAjax(worksDao.insert(works), "添加成功", "添加错误");
    }

    @Override
    public Result update(Works works) {
        return Result.toAjax(worksDao.updateById(works), "修改成功", "修改错误");
    }

    @Override
    public Result delete(Integer id) {
        return Result.toAjax(worksDao.deleteById(id), "删除成功", "删除错误");
    }

    @Override
    public Result getUserId(int id) {
        return Result.success(worksDao.selectList(new QueryWrapper<Works>().eq("create_user",id)));
    }
}
